/**
 * @作者 钟诚
 * @日期 2011-7-31
 * @时间 下午01:42:44
 * @描述
 * @版本
 */
package my.utils;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import my.commons.ReadCfg;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @作者 钟诚
 * @日期 2011-7-31
 * @时间 下午01:42:44
 * @描述
 * @版本 v1.0
 */
public class Toolkit {
	private static final Log log = LogFactory.getLog(Toolkit.class);

	/**
	 * 
	 * @描述 日志记录JVM内存使用情况
	 * @作者 钟诚
	 * @日期 2011-7-31
	 * @时间 下午02:15:45
	 * @param delay
	 * @param period
	 */
	public static void jvmMonitor() {
		long period = Long.parseLong(ReadCfg.getProp("JVMMonitorTime"));
		final Timer timer = new Timer();
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				Runtime runtime = Runtime.getRuntime();
				log.debug("totalMemory:" + runtime.totalMemory() / Math.pow(1024, 2) + "MB");
				log.debug("freeMemory:" + runtime.freeMemory() / Math.pow(1024, 2) + "MB");
				log.debug("maxMemory:" + runtime.maxMemory() / Math.pow(1024, 2) + "MB");
				log.debug("availableProcessors:" + runtime.availableProcessors());
			}
		};
		if (period > 0) {
			timer.schedule(task, 10000, period);
			Runtime.getRuntime().addShutdownHook(new Thread() {
				@Override
				public void run() {
					timer.cancel();
				}
			});
		} else {
			timer.cancel();
		}
	}

	public static <T> Object[][] getListBean2ObjectArrays(List<T> lstBean) {
		Object[][] objs = new Object[lstBean.size()][];
		for (int i = 0; i < lstBean.size(); i++)
			for (T bean : lstBean) {
				Object[] obj = null;
				try {
					obj = BeanUtils.describe(bean).values().toArray();
					System.out.println("================" + my.utils.StrUtil.toString(bean));
				} catch (Exception e) {
					log.error("listbean转Object二维数组异常:", e);
				}
				objs[i] = obj;
				log.debug(objs[i][2]);
				log.debug(objs[i][3]);
			}
		return objs;
	}
}
